Page({
  data: {
    list: [],
    current: 1,
    pageTotal: 1,
  },

  // 添加一条报修信息到列表的最上面
  addRepairItemToListTop(data) {
    this.setData({
      list: [data, ...this.data.list],
    })
  },

  // 更新某项的状态
  updateRepairItemStatus(id, status) {
    const index = this.data.list.findIndex((v) => v.id === id)
    this.setData({
      [`list[${index}].status`]: status,
    })
  },

  // 更新某项数据
  updateRepairItem(data) {
    // const repairItem = this.data.list.find(v => v.id === data.id)
    const index = this.data.list.findIndex((v) => v.id === data.id)
    this.setData({
      [`list[${index}]`]: data,
    })
  },

  onScrollToLower() {
    if (this.data.current >= this.data.pageTotal) return
    this.setData({ current: this.data.current + 1 })
    this.getList(this.data.current, 10)
  },

  goDetail(e) {
    wx.navigateTo({
      url: '/repair_pkg/pages/detail/index?id=' + e.mark.id,
    })
  },
  addRepair() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index',
    })
  },

  async getList(current, pageSize) {
    const res = await wx.http({
      method: 'get',
      url: '/repair',
      data: {
        current,
        pageSize,
      },
    })
    this.setData({
      list: [...this.data.list, ...res.data.rows],
      pageTotal: res.data.pageTotal,
    })
  },

  onLoad() {
    this.getList(1, 10)
    // if (this.data.list.length === 0) {
    //   this.getList(1, 10)
    // } else {
    //   // 刷新已加载过的列表
    //   const len = this.data.list.length
    //   this.setData({ list: [] })
    //   this.getList(1, len)
    //   // 原来，是每页10条，加载三次，30条
    //   // 现在，每夜30条，加载一次，30条
    // }
  },
})
